package com.example.exceldemo.demo.jfreechart;

import java.awt.*;
import java.util.Random;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;

/**
 * 竖型折线图
 */
public class VerticalLineChartDemo {

    public static void main(String[] args) {

        // 创建数据集
        CategoryDataset dataset = createDataset();

        // 创建图表
        JFreeChart chart = createChart(dataset);

        ChartPanel chartPanel = new ChartPanel(chart);
        JFrame frame = new JFrame();
        frame.setSize(600, 1000);
        frame.add(chartPanel);
        frame.setVisible(true);
    }

    // 创建数据集
    private static CategoryDataset createDataset() {
        Random random = new Random();
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int i = 0; i < 40; i++) {
            dataset.addValue(i % 2 == 0 ? -random.nextInt(6) : random.nextInt(10), "X坐标累计变化", Integer.toString(i));
        }
        return dataset;
    }

    // 创建图表
    private static JFreeChart createChart(CategoryDataset dataset) {
        JFreeChart chart = ChartFactory.createLineChart(
                "累计位移变化量",           // 图表标题
                "深度(m)",
                "变化量(mm)",
                dataset,           // 数据集
                PlotOrientation.HORIZONTAL,  // 图表方向
                true,              // 是否显示图例
                true,              // 是否生成工具
                false              // 是否生成 URL 链接
        );
        // 配置字体（解决中文乱码的通用方法）
        // 解决x轴标题中文乱码
        Font xfont = new Font("宋体", Font.PLAIN, 20); // X轴
        Font yfont = new Font("宋体", Font.PLAIN, 20); // Y轴
        Font kfont = new Font("宋体", Font.PLAIN, 20); // 底部
        Font titleFont = new Font("宋体", Font.PLAIN, 25); // 图片标题
        chart.setBackgroundPaint(Color.white);
        CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
        categoryPlot.getDomainAxis().setLabelFont(xfont);
        categoryPlot.getRangeAxis().setLabelFont(yfont);
        chart.getLegend().setItemFont(kfont);
        chart.getTitle().setFont(titleFont);

        // 定制图表样式
        CategoryPlot plot = chart.getCategoryPlot();
        // 设置网格背景颜色
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinesVisible(true);
        // 设置网格横线颜色
        plot.setDomainGridlinePaint(Color.black);
        // 设置网格竖线颜色
        plot.setRangeGridlinePaint(Color.gray);
        // 设置曲线图与xy轴的距离
        plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));


        //横纵轴设置
        CategoryAxis categoryAxis = plot.getDomainAxis();
        categoryAxis.setLowerMargin(0.01);
        categoryAxis.setUpperMargin(0.01);

        ValueAxis rangeAxis = plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setRange(-30, 30);
        rangeAxis.setAutoRangeMinimumSize(10);

        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        //renderer.setDefaultShapesVisible(true);
        renderer.setDefaultItemLabelGenerator(new CustomLabelGenerator());
        renderer.setDefaultItemLabelsVisible(true);

        return chart;
    }
}

class CustomLabelGenerator implements CategoryItemLabelGenerator {

    @Override
    public String generateRowLabel(CategoryDataset categoryDataset, int i) {
        return null;
    }

    @Override
    public String generateColumnLabel(CategoryDataset categoryDataset, int i) {
        return null;
    }

    @Override
    public String generateLabel(CategoryDataset categoryDataset, int i, int i1) {
        Number value = categoryDataset.getValue(i, i1);
        return value.toString();
    }
}